package com.ucombuy.search.service;

import com.github.shyiko.mysql.binlog.BinaryLogClient;
import com.github.shyiko.mysql.binlog.event.DeleteRowsEventData;
import com.github.shyiko.mysql.binlog.event.EventData;
import com.github.shyiko.mysql.binlog.event.UpdateRowsEventData;
import com.github.shyiko.mysql.binlog.event.WriteRowsEventData;

/**
 * Created by yaosheng on 2021/4/2.
 * 使用第三方组建监控MySQL数据库示例代码
 */
public class BinlogTestService {

    public static void main(String[] args) throws Exception{

        BinaryLogClient client = new BinaryLogClient
                ("127.0.0.1",3306,"root","YaoSheng0216");
        client.registerEventListener (event -> {
            EventData data = event.getData ();
            if(data instanceof UpdateRowsEventData){
                System.out.println ("Update ---------------");
                System.out.println (data.toString ());
            }else if(data instanceof WriteRowsEventData){
                System.out.println ("Write ---------------");
                System.out.println (data.toString ());
            }else if(data instanceof DeleteRowsEventData){
                System.out.println ("Delete ---------------");
                System.out.println (data.toString ());
            }
        });
        client.connect ();
    }

}
